package com.iraeta.controller.xiangmu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iraeta.common.Result;
import com.iraeta.controller.BaseController;
import com.iraeta.entity.resulttype.XiangmuRt;
import com.iraeta.entity.xiangmu.Gyspj;
import com.iraeta.entity.xiangmu.Xiangmu;
import com.iraeta.service.xiangmu.GyspjService;
import com.iraeta.service.xiangmu.XiangMuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController(value = "xiangMuController")
@RequestMapping("xiangmu")
public class XiangMuController extends BaseController {
    @Autowired
    XiangMuService xiangMuService;
    @Autowired
    GyspjService gyspjService;

    @RequestMapping("pagexiangmu")
    public String pageXiangMu(@RequestBody JSONObject jo){
        this.pageSet(jo);
        List<XiangmuRt> xiangmuList = xiangMuService.listAll();
        PageInfo<XiangmuRt> xiangMuPageInfo = this.getPageInfo(xiangmuList);
        return JSON.toJSONString(Result.success(xiangMuPageInfo));
    }

    @RequestMapping("getxiangmu")
    public String getXiangMu(@RequestBody JSONObject paramJb){
        int xmId = paramJb.getIntValue("id");
        Xiangmu xiangmu = xiangMuService.getXiangmu(xmId);
        return JSON.toJSONString(Result.success(xiangmu));
    }

    @RequestMapping("addeditxiangmu")
    public String addXiangMu(@RequestBody JSONObject paramJb) throws Exception{
        int xmId = paramJb.getIntValue("id");
        Xiangmu xiangmu = xmId <= 0 ? new Xiangmu() :  xiangMuService.getXiangmu(xmId);
        this.mapJsonToObj(paramJb, xiangmu);
        if(xmId <= 0){
            xiangmu.setCreateby(this.getUserId());
            xiangmu.setCreatetime(new Date());
            xiangMuService.addXiangmu(xiangmu);
        }else {
            xiangMuService.updateXiangmu(xiangmu);
        }
        return JSON.toJSONString(Result.success());
    }

    @RequestMapping("deletexiangmu")
    public String deleteXiangMu(@RequestBody JSONObject paramJb){
        int xmId = paramJb.getIntValue("id");
        xiangMuService.deleteXiangmu(xmId);
        return JSON.toJSONString(Result.success());
    }

    /*项目评价*/
    @RequestMapping("addeditxiangmupj")
    public String addXiangMuPj(@RequestBody JSONObject paramJb) throws Exception{
        Gyspj gyspj = new Gyspj();
        this.mapJsonToObj(paramJb, gyspj);
        gyspj.setCreatetime(new Date());
        gyspjService.addGyspj(gyspj);
        return JSON.toJSONString(Result.success());
    }
    @RequestMapping("deletexiangmupj")
    public String deleteXiangMupj(@RequestBody JSONObject paramJb){
        int xmId = paramJb.getIntValue("id");
        gyspjService.deleteGyspj(xmId);
        return JSON.toJSONString(Result.success());
    }
    @RequestMapping("listxiangmupj")
    public String listxiangmupj(@RequestBody JSONObject paramJb){
        int xmId = paramJb.getIntValue("id");
        List<Gyspj> xiangmupjList = gyspjService.listXmGyspj(xmId);
        return JSON.toJSONString(Result.success(xiangmupjList));
    }
}
